package com.adcc.soundalarm.fx.main;

import com.adcc.soundalarm.service.RequestService;
import com.adcc.soundalarm.sound.SoundAlarm;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.Optional;

/**
 * Created by ZHANG on 2017/3/23.
 */
public class MainView extends Application{

    private TrayIcon trayIcon;

    private MainViewController mainViewController;

    @Override
    public void start(final Stage stage) throws Exception {
        enableTray(stage);
        FXMLLoader loader = new FXMLLoader(getClass().getResource("main_view.fxml"));
        Parent root = loader.load();
        mainViewController = loader.getController();
        mainViewController.buildEnvironment(stage);
        location(stage);
        stage.setTitle("告警配置");
        stage.initStyle(StageStyle.UTILITY);
        stage.setResizable(false);
        stage.setOnCloseRequest(new EventHandler<WindowEvent>(){
            @Override
            public void handle(WindowEvent event) {
                event.consume();
                stage.close();
            }
        });
        stage.setScene(new Scene(root, 810, 820));
        mainViewController.init();
    }

    //右小角,最小化.
    private void enableTray(final Stage stage) {
        SystemTray systemTray = SystemTray.getSystemTray();
        PopupMenu popupMenu = new PopupMenu();
        MenuItem openSysItem = new MenuItem("系统配置");
        MenuItem openAlarmItem = new MenuItem("告警配置");
        MenuItem hideItem = new MenuItem("同步用户");
        MenuItem showAlarm = new MenuItem("查看日志");
        MenuItem quitItem = new MenuItem("退出程序");

        ActionListener actionListener = new ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                MenuItem item = (MenuItem) e.getSource();

                if (item.getLabel().equals("告警配置")) {
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            if(!stage.isShowing()){
                                stage.show();
                            }
                        }
                    });
                    return;
                }
                if (item.getLabel().equals("系统配置")) {
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            mainViewController.showSysAlarmDialog();
                        }
                    });
                    return;
                }
                if (item.getLabel().equals("查看日志")) {
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                          mainViewController.showAlarmDialog();
                        }
                    });
                    return;
                }
                if (item.getLabel().equals("同步用户")) {
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            Alert _alert = new Alert(Alert.AlertType.INFORMATION,"",new ButtonType("取消", ButtonBar.ButtonData.NO),
                                    new ButtonType("确定", ButtonBar.ButtonData.YES));
                            _alert.setHeaderText("是否同步用户");
                            Optional<ButtonType> _buttonType = _alert.showAndWait();
                            if(_buttonType.get().getButtonData().equals(ButtonBar.ButtonData.YES)){
                                mainViewController.sysncUser();
                            }
                        }
                    });
                    return;
                }
                if (item.getLabel().equals("退出程序")) {
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            Alert _alert = new Alert(Alert.AlertType.WARNING,"",new ButtonType("取消", ButtonBar.ButtonData.NO),
                                    new ButtonType("确定", ButtonBar.ButtonData.YES));
                            _alert.setHeaderText("是否退出程序");
                            Optional<ButtonType> _buttonType = _alert.showAndWait();
                            if(_buttonType.get().getButtonData().equals(ButtonBar.ButtonData.YES)){
                                mainViewController.dispose();
                                RequestService.getInstance().dispose();
                                SoundAlarm.getInstance().dispose();
                                System.exit(0);
                            }
                        }
                    });
                    return;
                }
            }
        };

        //双击事件方法
        MouseListener sj = new MouseListener() {
            public void mouseReleased(MouseEvent e) {
            }
            public void mousePressed(MouseEvent e) {
            }
            public void mouseExited(MouseEvent e) {
            }
            public void mouseEntered(MouseEvent e) {
            }
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            if(!stage.isShowing()){
                                stage.show();
                            }
                        }
                    });
                }
            }
        };
        openSysItem.addActionListener(actionListener);
        openAlarmItem.addActionListener(actionListener);
        quitItem.addActionListener(actionListener);
        showAlarm.addActionListener(actionListener);
//        hideItem.addActionListener(actionListener);

        popupMenu.add(openSysItem);
        popupMenu.add(openAlarmItem);
//        popupMenu.add(hideItem);
        popupMenu.add(showAlarm);
        popupMenu.add(quitItem);

        try {
            BufferedImage image = ImageIO.read(MainView.class.getResourceAsStream("icon.png"));
            trayIcon = new TrayIcon(image, "告警工具", popupMenu);
            trayIcon.setToolTip("告警工具");
            trayIcon.addMouseListener(sj);
            Platform.setImplicitExit(false); //多次使用显示和隐藏设置false
            systemTray.add(trayIcon);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void location(Stage stage){
        Dimension dimenson = Toolkit.getDefaultToolkit().getScreenSize();
        stage.setWidth(806);
        stage.setHeight(545);
        double x = (dimenson.width - stage.getWidth())/2 ;
        double y = (dimenson.height - stage.getHeight())/2;
        if(dimenson.width - x - stage.getWidth() < 510){
            x = dimenson.width - 510 - stage.getWidth();
        }
        stage.setX(x);
        stage.setY(y);
    }
}
